package cn.newgrand.netcall.imp.advertisement;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.util.List;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

import org.jivesoftware.Spark;
import org.jivesoftware.resource.Default;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.browser.BrowserFactory;
import org.jivesoftware.spark.util.BrowserLauncher;
import org.jivesoftware.spark.util.GraphicUtils;

import com.sun.media.Log;

import cn.newgrand.netcall.advertisement.AdXML;
import cn.newgrand.netcall.advertisement.Advertisement;
import cn.newgrand.netcall.advertisement.AdvertisementPanel;

/**
 * 主界面底部广告类
 * 
 * @author SidneyRen
 * 
 */
public class AdMainWin extends AdvertisementPanel {

	List<Advertisement> adList = AdXML.getInstance().getAdMainWinList();
	private JLabel imageLabel = new JLabel();
	private Image backgroundImage;
	private Advertisement ad;
	private static int adNum = 0;

	public AdMainWin() {
	
		loadAd();
		
//		getAdLabel().setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/ad/advertisement.gif")));
		Log.warning("getadLabel: "+getAdLabel().getIcon().getIconHeight());
		
		add(getAdLabel(), new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		
	}

	/**
	 * 更换主面板广告图片
	 * 
	 * 并调用父类中的fillAd方法来设置ad、刷新面板
	 * 
	 * @param ad
	 */

	public void fillMainWinAd(Advertisement ad) {
		if(null==ad.getImageIcon()) return;
//		getAdLabel().setText(ad.getTitle());
		getAdLabel().setIcon(ad.getImageIcon());
		fillAd(ad);
	}
	
	/**
	 * 加载主窗体中的广告
	 */

	public void loadAd() {
		if (adList.size() == 0)
			return;

		if (adList.size() > adNum)
			fillMainWinAd(adList.get(adNum));
		else{
			adNum = 0;
			fillMainWinAd(adList.get(adNum));
		}

	}

	
	/**
	 * 更新主窗体广告
	 * 
	 */
	public void updateAd() {
		if(adList.size()==0)
			return;
		if (adList.size() > adNum) {
			fillMainWinAd(adList.get(adNum));
			adNum += 1;
		} else {
			adNum = 0;
			fillMainWinAd(adList.get(adNum));
		}
		
	}

}
